// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { AlertManager } from "../../alert/AlertManager";
import { Common_UIPath } from "../../common/Common_Define";
import { Notifier } from "../../framework/Notifier";
import { UIManager } from "../../framework/UIManager";
import GlobalData from "../../GlobalData";
import { ListenID } from "../../ListenID";
import { Util } from "../../utils/Util";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PrizePoolItem extends cc.Component {

    @property(cc.Sprite)
    headImge: cc.Sprite = null;

    @property(cc.Label)
    time: cc.Label = null;

    @property(cc.Label)
    nick_name: cc.Label = null;

    @property(cc.Button)
    Challenges: cc.Button = null;
    prizeData: any;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    registerTouch() {
        // if
    }
    start () {

    }
    setData(data) {
        this.prizeData = data;
        this.node.active = true;

        if (data.avatar) {
            Util.loadRemoteSf(data.avatar, this.headImge);
        }

        if (String(data.user_nickname).length > 10) {
            this.nick_name.string = String(data.user_nickname).substring(0, 10) + "...";
        } else {
            this.nick_name.string = data.user_nickname || "匿名玩家";
        }

        this.time.string = Util.convertTimestampToDateTime(Number(data.create_time)) || `--`;
    }


    onClickTiaozhan() {
        if (GlobalData.inst.uid == this.prizeData.uid) {
            AlertManager.showNormalTips("不能挑战自己");
            return;
        }
        if(GlobalData.inst.findMe_prize){
            AlertManager.showNormalTips("你已经加入分红");
            return;
        }
        this.onClose();
        GlobalData.inst.tiaozhandata.default_gunId = this.prizeData.default_gunId || 105;
        GlobalData.inst.tiaozhandata.battle = Number(this.prizeData.battle) || 100;
        GlobalData.inst.tiaozhandata.uid = this.prizeData.uid || "";
        GlobalData.inst.tiaozhandata.baseAllHp = Number(this.prizeData.baseAllHp) || 300;
        GlobalData.inst.tiaozhandata.type = "prizePool";
        console.log("进入挑战房间", GlobalData.inst.tiaozhandata);
        cc.director.loadScene("GameTiaozhan");
    }

    public onClose(): void {
        UIManager.Close(Common_UIPath.RevenueView);
        UIManager.Close(Common_UIPath.MenuUI);
        UIManager.Close(Common_UIPath.CurrencyUI);
        this.unscheduleAllCallbacks();
        Notifier.send(ListenID.Menu_Open, false);
        Notifier.send(ListenID.On_Close_Menu);
    }


    // update (dt) {}
}
